﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;

namespace WebApplication1
{
    public partial class viewTeamById : System.Web.UI.Page
    {
        SportsAL sportsAL;
        string queryString;
        Guid teamId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                queryString = Request.QueryString.ToString();
                teamId = Guid.Empty;

                bool convertableToGuid = Guid.TryParse(queryString, out teamId);
                if (convertableToGuid)
                {
                    CreateDynamicTable(teamId);

                }
                else
                {
                    //Wrong Committee Id
                }


            }
        }
            
        
        private void CreateDynamicTable(Guid teamId)
        {
            PlaceHolder6.Controls.Clear();


            sportsAL = new SportsAL();
            StudentAL studentAL = new StudentAL();

            List<TeamDTO> teamDTO = sportsAL.GetTeamById(teamId);
            List<TeamMemberDTO> teamMemberDTO = sportsAL.GetTeamMemberByTeamId(teamId).ToList();

            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder6.Controls.Add(tbl);


            Table subtbl = new Table();
            subtbl.Width = new Unit("50%");
            subtbl.BorderStyle = BorderStyle.Solid;

            Label lblTeamName = new Label();
            lblTeamName.Text = "Team Name";

            Label lblTeamYear = new Label();
            lblTeamYear.Text = "Year";

            Label lblTeamGameType = new Label();
            lblTeamGameType.Text = "Game Type";

            Label lblTeamAgeDivision = new Label();
            lblTeamAgeDivision.Text = "Age Division";

            Label lblTeamDescription = new Label();
            lblTeamDescription.Text  = "Description";


            List<Label> allHeaderLabels = new List<Label>();

            allHeaderLabels.Add(lblTeamName);
            allHeaderLabels.Add(lblTeamYear);
            allHeaderLabels.Add(lblTeamGameType);
            allHeaderLabels.Add(lblTeamAgeDivision);
            allHeaderLabels.Add(lblTeamDescription);
            

            int i = 0;
            foreach (Label lblHeader in allHeaderLabels)
            {


                TableRow trHeader = new TableRow();
                trHeader.Width = new Unit("50%");

                TableCell tcHeader = new TableCell();
                TableCell tcHeaderValue = new TableCell();

                Label lblValue = new Label();
                if (i % 2 == 0)
                {
                    tcHeader.BackColor = System.Drawing.Color.Blue;
                    tcHeader.ForeColor = System.Drawing.Color.White;

                    tcHeaderValue.BackColor = System.Drawing.Color.Blue;
                    tcHeaderValue.ForeColor = System.Drawing.Color.White;

                }
                else
                {
                    tcHeader.BackColor = System.Drawing.Color.White;
                    tcHeader.ForeColor = System.Drawing.Color.Blue;

                    tcHeaderValue.BackColor = System.Drawing.Color.White;
                    tcHeaderValue.ForeColor = System.Drawing.Color.Blue;
                }

                if (lblHeader.Text == "Team Name")
                {
                    lblValue.Text = teamDTO[0].TeamName;
                }

                else if (lblHeader.Text == "Year")
                {
                    lblValue.Text = teamDTO[0].Year.ToString();
                }

                else if (lblHeader.Text == "Game Type")
                {
                    lblValue.Text = teamDTO[0].GameType;
                }

                else if (lblHeader.Text == "Age Division")
                {
                    lblValue.Text = teamDTO[0].AgeDivision;
                }
                 else if (lblHeader.Text == "Description")
                {
                    lblValue.Text = teamDTO[0].Description;
                }
                /* else if (lblHeader.Text == "Contribution")
                {
                    lblValue.Text = committeeDTO[0].Contribution;
                }*/
                tcHeader.Controls.Add(lblHeader);
                tcHeaderValue.Controls.Add(lblValue);
                trHeader.Cells.Add(tcHeader);
                trHeader.Cells.Add(tcHeaderValue);


                tbl.Rows.Add(trHeader);
                i++;
            }

            int tblRows = teamMemberDTO.Count;


            // Now iterate through the table and add your controls 
            for (int j = 0; j < tblRows; j++)
            {
                List<StudentDTO> studentDTO = studentAL.GetStudentById(teamMemberDTO[j].TeamMemberId);
                TableRow trBody = new TableRow();
                trBody.Width = new Unit("50%");

                TableCell tcBodyDesignation = new TableCell();
                TableCell tcBodyMemberName = new TableCell();

                Label lblDesignation = new Label();
                HyperLink hlMemberName = new HyperLink();
                if (i % 2 == 0)
                {
                    tcBodyDesignation.BackColor = System.Drawing.Color.DarkSlateBlue;
                    tcBodyDesignation.ForeColor = System.Drawing.Color.White;

                    tcBodyMemberName.BackColor = System.Drawing.Color.DarkSlateBlue;
                    tcBodyMemberName.ForeColor = System.Drawing.Color.White;

                }
                else
                {
                    tcBodyDesignation.BackColor = System.Drawing.Color.White;
                    tcBodyDesignation.ForeColor = System.Drawing.Color.DarkSlateBlue;

                    tcBodyMemberName.BackColor = System.Drawing.Color.White;
                    tcBodyMemberName.ForeColor = System.Drawing.Color.DarkSlateBlue;
                }

               /* if ((tea[j].Position == "Vice Patron") ||( committeeMemberDTO[j].Position == "Vice President" )||( committeeMemberDTO[j].Position == "Committee Member"))
                {
                    lblDesignation.Text = committeeMemberDTO[j].Position + "(s)";
                    hlMemberName.Text = memberDTO[0].FullName;
                    hlMemberName.NavigateUrl = "viewMember.aspx?" + memberDTO[0].MemberId;

                    tcBodyDesignation.Controls.Add(lblDesignation);
                    tcBodyMemberName.Controls.Add(hlMemberName);
                    
                    
                }
                else
                {*/

                    lblDesignation.Text = teamMemberDTO[j].Role;
                    hlMemberName.Text = teamMemberDTO[j].StudentFullName;
                    hlMemberName.NavigateUrl = "viewAllTeamParticipationOfStudent.aspx?" + teamMemberDTO[0].TeamMemberId;


                    tcBodyDesignation.Controls.Add(lblDesignation);
                    tcBodyMemberName.Controls.Add(hlMemberName);
                //}

                trBody.Cells.Add(tcBodyDesignation);
                trBody.Cells.Add(tcBodyMemberName);



                tbl.Rows.Add(trBody);
                i++;
            }

            // Add the TableRow to the Table

            //tbl.Rows.Add(trBody);
            // }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
            hlViewAllMatchesOfThisTeam.NavigateUrl = "viewAllMatches.aspx?" + teamId;
            //hlViewAllMeetingOfCommittee.NavigateUrl = "viewAllMeeting.aspx?" + committeeId;
            //hlViewAllMinutesOfCommittee.NavigateUrl = "viewAllMintes.aspx?" + committeeId;
        
        }



    }
}